<template>
  <div :class="className" :style="{height:height,width:width}" >
<!--    {{ proLsit }} -->

  </div>
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from './mixins/resize'

  // const animationDuration = 6000

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '50%'
      },
      height: {
        type: String,
        default: '300px'
      },
      title: {
        type: String,
        default: ''
      },
      proLsit: {
        type: Array,
        default: []
      },
      storageDes: {
        type: Function,
        default: null
      }
    },
    data() {
      return {
        chart: null,
        goodsNames: []
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        // var xAx = [],
        //   yAx = []
        // this.proLsit.forEach(element => {
        //   xAx.push(element.catName)
        //   yAx.push(element.proAmount)

        // })
        var xAx = [],
            yAx = [],
            dataList = [],
            code = {}
        this.proLsit.forEach((item, index) => {
          xAx.push(item.countyName)
          code[item.countyName] = item.areacode
          // console.log('code', code)
          // code.push(item.areacode)
          console.log(item.list)
          item.list.forEach((i, j) => {
            yAx.push(i.proAmount)
            dataList.push(i.catName)
          })
        })
        console.log(dataList)
        this.chart = echarts.init(this.$el, 'macarons')

        this.chart.setOption({
          legend: {
            //图例
            data: dataList, //与series的name对应
            left: "75%", //图例的位置，可以用像素，可以用百分比，也可以用center，right等
            top: 12.5, //图例的位置
            itemWidth: 10, //图例图标的宽
            itemHeight: 10, //图例图标的高
            textStyle: {
              color: "#000111" //值的具体的颜色
            }
          },
          title: {
            text: this.title,
            left: "center"
          },
          tooltip: {},
          xAxis: {
            type: "category",
            data: xAx,
            axisTick: {
              //刻度
              show: false //不显示刻度线
            },
            splitLine: {
              show: false //去掉分割线
            }
          },
          yAxis: {
            type: "value",
            name: "单位：台",
            minInterval: 10, //标值的最小间隔
            max: 100 //最大刻度
          },
          series: [{
            name: "销量",
            type: "bar",
            data: yAx,
            barWidth: 20
          }],
          color: ["#66CCFF", "#0000FF"]
        })
        var that=this
        this.chart.on("click", function(params) {
          // code.forEach((item,index)=>{
          //   that.$set(params,'code',item)
          // })
          console.log(code[params.name])
          // console.log()
          // for(var i=0; i<code.length; i++){
          //   // console.log(code[i])

          // }
          // that.everyCode = code[params.name]
          that.storageDes(code[params.name])
          sessionStorage.setItem('sbcode',code[params.name])
        });

      },
    }
  }
</script>
